home *** CD-ROM | disk | FTP | other *** search
-
- package sub_arctic.constraints;
-
- import sub_arctic.lib.interactor;
- import sub_arctic.lib.interactor_consts;
- import sub_arctic.lib.manager;
- import sub_arctic.lib.sub_arctic_error;
-
- import java.util.Vector;
-
- /**
- * Constraint implementation class to provide encoding for standard 2 operand
- * lightweight constraints. This object takes 2 std_objpart_encoding objects
- * representing operands, along with a signed 15 bit value to provide a
- * constant to the function.<p>
- *
- * 2 operand constraints are encoded as:
- * <pre>
- * 15 6 6 5
- * KKKKKKKKKKKKKKK AAAAAA BBBBBB 00010 30 2 operand operations
- * ... [K is 15 bit signed]
- * KKKKKKKKKKKKKKK AAAAAA BBBBBB 11111
- * </pre>
- * where KKK represents a 15 bit signed constant, AAA, and BBB represent
- * parameter objects, and XXX represents bits used to encode an op code.<p>
- *
- * @version $Id: op2_impl.java,v 1.8 1996/10/03 19:43:09 hudson Exp $
- * @author Scott Hudson
- */
-
- public class op2_impl implements std_encoding_consts {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Create a standard constraint given an op_code, 2 obj/part designators,
- * and a constant.
- *
- * @param int op_code op code value for this operation.
- * @param std_objpart_encoding op1 designator for operand 1
- * @param std_objpart_encoding op2 designator for operand 2
- * @param short K value for 15 bit signed constant
- */
- public static std_constraint create(
- int op_code, std_objpart_encoding op1, std_objpart_encoding op2, short K)
- {
- return new std_constraint(encode(op_code,op1, op2, K),
- op1.orientation() | op2.orientation());
- }
-
- //had:
- //* @exception bad_constraint if the op code is out of range.
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Do an encoding given an op_code, 2 obj/part designators, and a constant.
- *
- * @param int op_code op code value for this operation.
- * @param std_objpart_encoding op1 designator for operand 1
- * @param std_objpart_encoding op2 designator for operand 2
- * @param short K value for 15 bit signed constant
- */
- public static int encode(
- int op_code, std_objpart_encoding op1, std_objpart_encoding op2, short K)
- {
- if (op_code < OP2_MIN || op_code > OP2_MAX)
- throw new sub_arctic_error(
- "Unrecognized op code (" + op_code + ") used for 2 op constraint");
-
- return (((int)K)<<17) | ((op1.encoding() & OBJPART_MASK) << 11) |
- ((op2.encoding() & OBJPART_MASK) << 5) |
- ((op_code<<OP2_SHIFT) & OP2_MASK) | OP2_LOBITS;
- }
-
- //had:
- //* @exception bad_constraint if the op code is out of range.
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Decode op code from given encoding. This assumes we already know this
- * is a 2-op encoding -- otherwise we get the wrong answer.<p>
- *
- * @param int enc the encoding for a constraint.
- */
- public static int op_code(int enc) { return (enc & OP2_MASK) >> OP2_SHIFT; }
-
- /**
- * Decode first operand from given encoding. This assumes we already know
- * this is a 2-op encoding -- otherwise we get the wrong answer.<p>
- *
- * @param int enc the encoding for a constraint.
- */
- public static int op1(int enc) { return (enc >> 11) & OBJPART_MASK; }
-
- /**
- * Decode second operand from given encoding. This assumes we already know
- * this is a 2-op encoding -- otherwise we get the wrong answer.<p>
- *
- * @param int enc the encoding for a constraint.
- */
- public static int op2(int enc) { return (enc >> 5) & OBJPART_MASK; }
-
- /**
- * Decode constant from the given encoding. This assumes we already know
- * this is a 2-op encoding -- otherwise we get the wrong answer.<p>
- *
- * @param int enc the encoding for a constraint.
- */
- public static short const_val(int enc)
- {
- /* get the value out of the high 15 bits, sign extending with >> */
- return (short)(enc>>17);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /* Part constants copied over from interactor_consts for convenience. */
- public static final int X = interactor_consts.X;
- public static final int Y = interactor_consts.Y;
- public static final int W = interactor_consts.W;
- public static final int H = interactor_consts.H;
- public static final int VISIBLE = interactor_consts.VISIBLE;
- public static final int ENABLED = interactor_consts.ENABLED;
- public static final int PART_A = interactor_consts.PART_A;
- public static final int PART_B = interactor_consts.PART_B;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Evaluate an encoded constraint function given its operand and constant
- * values. We assume here that it is has already been determined that
- * the constraint is a 2-op constraint.<p>
- *
- * @param int enc the encoding from the constraint
- * @param interactor constr_obj the object being constrained
- * @param int val1 value of first operand
- * @param int val2 value of second operand
- * @param int cnst_val value of constant parameter
- * @param int orient orientation of the constraint (should be
- * HORIZONTAL or VERTICAL).
- * @return int the result of the evaluation
- */
- public static int eval_fun(
- int enc,
- interactor constr_obj,
- int val1,
- int val2,
- int cnst_val,
- int orient)
- {
- interactor par;
-
- /* sanity check */
- if (constr_obj == null)
- throw new sub_arctic_error("Null constrained object passed to " +
- "op2_impl.eval_fun()");
-
- /* execute code for the encoded function */
- switch(op_code(enc))
- {
- case OP_add: return val1 + val2 + cnst_val;
- case OP_subtract: return val1 - val2 + cnst_val;
- case OP_mult: return val1 * val2 + cnst_val;
- case OP_div: if (val2 != 0)
- return val1 / val2 + cnst_val;
- else
- return 0;
- case OP_mod: if (val2 != 0)
- return val1 % val2 + cnst_val;
- else
- return 0;
- case OP_and: return (val1 & val2) & (cnst_val | 0xffff8000);
- case OP_or: return (val1 | val2) & (cnst_val | 0xffff8000);
- case OP_xor: return (val1 ^ val2) & (cnst_val | 0xffff8000);
- case OP_min: return ((val1 < val2) ? val1 : val2) + cnst_val;
- case OP_max: return ((val1 > val2) ? val1 : val2) + cnst_val;
- case OP_ave: return ((val1 + val2)/2) + cnst_val;
- case OP_rotx: return 0; //xx later
- case OP_roty: return 0; //xx later
- case OP_if_enabled: if (constr_obj.enabled())
- return val1;
- else
- return val2;
- case OP_if_visible: if (constr_obj.visible())
- return val1;
- else
- return val2;
-
- case OP_self_fun2:
- /* use the object's custom_fun2() to compute the value */
- return constr_obj.custom_fun2(val1, val2, cnst_val);
-
- case OP_parent_fun2:
- /* get the parent and call its custom_fun2() */
- par = constr_obj.parent();
- if (par != null)
- return par.custom_fun2(val1, val2, cnst_val);
- else
- return 0;
-
- case OP_fill: return val2 - val1 - cnst_val;
-
- default:
- /* something is wrong if we get here */
- throw new sub_arctic_error("Improperly encoded constraint found in "+
- "op1_impl.eval_fun()");
- }
- }
-
- //had:
- //* @exception bad_value if the encoding, or constrained object/part are
- //* malformed
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Evaluate an encoded constraint applied to the given part of the given
- * object. We assume here that it it has already been determined that
- * the constraint is a 2-op constraint.<p>
- *
- * @param int enc the encoding from the constraint
- * @param interactor constr_obj the object being constrained
- * @param int constr_part the part being constrained
- * @param int orient the orientation of the constraint
- * @return int the result of the evaluation
- */
- public static int eval(
- int enc,
- interactor constr_obj,
- int constr_part,
- int orient)
- {
- int cnst_val, val1, val2;
- provider_part_ref ext;
- Object val;
- interactor par;
-
- /* sanity check */
- if (constr_obj == null)
- throw new sub_arctic_error("Null constrained object passed to " +
- "op2_impl.eval()");
-
- /* extract the constant */
- cnst_val = const_val(enc);
-
- /* get the value for the operands */
- val1 = std_constraint_impl.the_impl().fetch_value(
- op1(enc), constr_obj, orient);
- val2 = std_constraint_impl.the_impl().fetch_value(
- op2(enc), constr_obj, orient);
-
- /* evaluate the encoded function */
- return eval_fun(enc, constr_obj, val1, val2, cnst_val, orient);
-
- }
-
- //had:
- //* @exception bad_value if the encoding, or constrained object/part are
- //* malformed
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Test whether the given encoded constraint (constraining the given
- * object) depends on the indicated object and part (expressed relative
- * to it). Here we assume that the encoding is already known to contain
- * a 2-op constraint.<p>
- *
- * @param int enc The encoding for the constraint.
- * @param interactor constr_obj The object the constraint is attached to
- * @param int test_which_obj The object we are asking about. This
- * can be one of the values OBJCODE_SELF,
- * OBJCODE_PARENT, OBJCODE_SOME_CHILD,
- * OBJCODE_PREV_SIBLING, or
- * OBJCODE_NEXT_SIBLING.
- * @param int test_which_part The part we are asking about.
- * @param int nth_child for SOME_CHILD, this provides the index
- * of the child (and is ignored otherwise).
- * @param int orient Orientation of the constraint. This
- * should be HORIZONTAL or VERTICAL.
- * @return boolean whether the given constraint depends upon the given value
- */
- public static boolean depends_on(
- int enc,
- interactor constr_obj,
- int which_obj,
- int which_part,
- int nth_child,
- int orient)
- {
- int op;
-
- /* extract op code to look for special case implicit operands */
- op = op_code(enc);
-
- /* special cases here for if_enabled and if_visible */
- if (op == OP_if_enabled && which_obj == OBJCODE_SELF)
- {
- // test for which_part == enabled here xx
- }
- else if (op == OP_if_visible && which_obj == OBJCODE_SELF)
- {
- // test for which_part == enabled here xx
- }
-
- /* now have a look at the first operand dependency */
- op = op1(enc);
- if (std_constraint_impl.the_impl().part_depends_on(op, constr_obj,
- which_obj, which_part, nth_child, orient))
- return true;
-
- /* if that didn't match try the second operand */
- op = op2(enc);
- return std_constraint_impl.the_impl().part_depends_on(op, constr_obj,
- which_obj, which_part, nth_child, orient);
- }
-
- //had:
- //* @exception bad_value if one of the parameters has an unrecognized value
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Extract the set of objects/parts that a constraint depends on. This
- * produces a Vector with pairs of entries, the first being an interactor,
- * and the second being an Integer which is the part number of that
- * interactor that is depended upon.<p>
- *
- * Here we assume that this is a 2 operand constraint.<p>
- *
- * @param int enc encoding value for the constraint in question.
- * @param interactor constr_obj the object the constraint is attached to
- * (hence its referents are relative to).
- * @param int orient Orientation of the constraint. This
- * should be HORIZONTAL or VERTICAL.
- * @return Vector containing pairs of objects, the first being an interactor
- * which is depended upon, and the second being an Integer
- * giving the part number of the part depended upon.
- */
- public static Vector mk_depend_list(int enc, interactor constr_obj,int orient)
- {
- int op;
- Vector result;
-
- /* do the implicit dependencies first */
- result = mk_implicit_depend_list(enc, constr_obj, orient);
-
- /* extract the first operand and process that */
- op = op1(enc);
- std_constraint_impl.the_impl().
- add_depend_obj_part(result, op, constr_obj, orient);
-
- /* and the second */
- op = op2(enc);
- std_constraint_impl.the_impl().
- add_depend_obj_part(result, op, constr_obj, orient);
-
- return result;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Extract the set of objects/parts that a particular constraint function
- * implicitly depends on. This produces a Vector with pairs of entries, the
- * first being an interactor, and the second being an Integer which is the
- * part number of that interactor that is depended upon.<p>
- *
- * @param int enc encoding value for the constraint in question.
- * @param interactor constr_obj the object the constraint is attached to
- * (hence its referents are relative to).
- * @param int orient Orientation of the constraint. This
- * should be HORIZONTAL or VERTICAL.
- * @return Vector containing pairs of objects, the first being an interactor
- * which is depended upon, and the second being an Integer
- * giving the part number of the part depended upon.
- */
- public static Vector mk_implicit_depend_list(
- int enc, interactor constr_obj,int orient)
- {
- int op;
- Vector result = new Vector(6);
-
- /* extract operand. */
- op = op_code(enc);
-
- /* OP_if_enabled implicitly depends on self.enabled */
- if (op == OP_if_enabled)
- {
- result.addElement(constr_obj);
- result.addElement(new Integer(ENABLED));
- }
- /* OP_if_visible implicitly depends on self.visible */
- else if (op == OP_if_visible)
- {
- result.addElement(constr_obj);
- result.addElement(new Integer(VISIBLE));
- }
-
- return result;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
-
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-